import { enemyPlane_slo } from './../component/enemyPlane';
import emitter from "../emitter";
import playerDataInfo from "../playerData";

const { ccclass, property } = cc._decorator;
export var enemyPlaneUI_slo: enemyPlaneUI;
@ccclass
export default class enemyPlaneUI extends cc.Component {
    @property(cc.Label)
    private txtHarm: cc.Label = null;
    @property(cc.ProgressBar)
    private booldProggress: cc.ProgressBar = null;

    @property(cc.Node)
    private nodeLen: cc.Node = null;
    private blood = 3;
    private isBoss = false;

    @property(cc.Node)
    private nodeCollider: cc.Node = null;
    private collider;
    onEnable() {
        this.initData();
    }

    initData() {
        enemyPlaneUI_slo = this;
        //@ts-ignore
        this.blood = playerDataInfo.gameData.level * 4;
        this.collider = this.nodeCollider.getComponent("enemyPlaneCollider");
        this.collider.setPlaneHarm = this.setBloodProgress.bind(this);
    }

    initAddListener() {
        // emitter.on("planeHarm", this.setBloodProgress.bind(this));
    }

    //根据血量显示进度条
    Invde = false;
    setBloodProgress(blood = 1, isShowHarm = true) {
        if (this.Invde) return
        this.blood -= blood;

        //@ts-ignore
        let allBlood = this.isBoss ? 40 : (playerDataInfo.gameData.level * 4);
        //总血量 - 当前血量 / 总血量
        //@ts-ignore
        let harmProgr = (allBlood - this.blood) / allBlood;
        this.booldProggress.progress = 1 - harmProgr;
        if (isShowHarm) this.setHarmAnima(blood);
        if (this.blood <= 0) {
            this.Invde = true;
            enemyPlane_slo.onPlaneDie(this.node);
            if (this.isBoss) emitter.emit("bossDie");
        }
    }

    planeIsBoos(isBoos) {
        this.isBoss = isBoos;
        this.blood = 40;
    }

    //显示受到伤害
    setHarmAnima(harm) {
        this.txtHarm.string = "-" + harm;
        cc.tween(this.txtHarm.node)
            .to(0.2, { scale: 1.2 })
            .delay(0.1)
            .to(0.1, { scale: 0 })
            .start()
    }

    isAttack = false;
    update() {
        if (this.isBoss && !this.isAttack) {
            if (this.node.y < cc.winSize.height / 2 - this.node.width / 2) {
                this.isAttack = true;
                emitter.emit("gameStart", true);
            }
        }
    }
}
